package com.rxst.msg.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.rxst.msg.base.ResponseVo;
import com.rxst.msg.dto.save.MsgxIpSaveDto;
import com.rxst.msg.dto.search.MsgxIpSearchDto;
import com.rxst.msg.dto.update.MsgxIpUpdateDto;
import com.rxst.msg.entity.MsgxIpEntity;
import com.rxst.msg.server.MsgxIpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 白名单
 *
 * @author rxst
 * @email rxst@gmail.com
 * @date 2021-08-19 11:47:01
 */
@RestController
@RequestMapping("ip")
@Api(value = "白名单", tags = "白名单")
@Slf4j
public class MsgxIpController {

    @Autowired
    private MsgxIpService service;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("列表")
    public ResponseVo List(@RequestBody MsgxIpSearchDto dto) {
        return ResponseVo.ok(service.list(dto));
    }

    /**
     * 分页Function
     */
    @PostMapping("/page")
    @ApiOperation("分页")
    public ResponseVo page(@RequestBody MsgxIpSearchDto dto) {
        return ResponseVo.ok(service.page(dto));
    }

    /**
     * 详情
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseVo info(String id) {
        return ResponseVo.ok(service.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存")
    public ResponseVo save(@RequestBody MsgxIpSaveDto dto) {
        //校验类型
        MsgxIpEntity entity = BeanUtil.toBean(dto, MsgxIpEntity.class);
        return ResponseVo.ok(service.saveEntity(entity));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody MsgxIpUpdateDto dto) {
        MsgxIpEntity entity = BeanUtil.toBean(dto, MsgxIpEntity.class);
        return ResponseVo.ok(service.updateEntity(entity));
    }
    /**
     * 更新缓存
     */
    @GetMapping("/cache")
    @ApiOperation("更新缓存")
    public ResponseVo updateCache() {
        service.initCache();
        return ResponseVo.ok();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("批量删除")
    public ResponseVo delete(@RequestParam(value = "ids") List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            service.removeByIds(ids);
        } else {
            return ResponseVo.error("参数不能为空");
        }
        return ResponseVo.ok();
    }

    /**
     * 批量逻辑删除
     */
    @DeleteMapping("/delete/logic")
    @ApiOperation("批量逻辑删除")
    public ResponseVo deleteLogic(@RequestParam(value = "ids") List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            service.deleteLogicByIds(ids);
        } else {
            return ResponseVo.error("参数不能为空");
        }
        return ResponseVo.ok();
    }
}
